import { Component, Input, OnInit } from '@angular/core';
import { FetchService } from '../../../core/net/fetch.service';
import { NzMessageService, NzModalRef } from 'ng-zorro-antd';
import { Router } from '@angular/router';

const XunHash = {
    'F': '上',
    'S': '中',
    'T': '下'
};
/**
 * 数据报送 一键催报url列表
 */
const reportUrls = {
    '1': 'muw/submit/reportWf',
    '2': 'muw/submit/reportIrr',
    '3': 'muw/submit/reportLw',
    '4': 'muw/submit/reportIrr',
    '5': ''
};

@Component({
    selector: 'app-dp-report-overview',
    templateUrl: './dp-report-overview.component.html',
    styleUrls: ['./dp-report-overview.component.scss']
})
export class DpReportOverviewComponent implements OnInit {
    @Input() mngType = 0;   // 默认为中心 0-中心 1-管理处
    type = 0;
    manageId = JSON.parse(localStorage.userInfo).manageId;
    reportTypeSelections = [
        {
            label: '数据报送',
            value: 0
        },
        {
            label: '数据发布',
            value: 1
        },
        {
            label: '计划报送',
            value: 2
        }
    ];
    tableData = [];

    mngReportStatus = {
        1: '未报送',
        2: '已报送'
    };
    mngPlanStatus = {
        0: '未报送',
        1: '已报送'
    };

    constructor(private fetchService: FetchService,
                private message: NzMessageService,
                private router: Router,
                private modal: NzModalRef) {
    }

    ngOnInit(): void {
        this.reportTypeChanged(0);
        if (this.mngType === 0) {
            this.reportTypeSelections = [
                {
                    label: '数据报送',
                    value: 0
                },
                {
                    label: '数据发布',
                    value: 1
                },
                {
                    label: '计划报送',
                    value: 2
                }
            ];
        } else {
            this.reportTypeSelections = [
                {
                    label: '数据报送',
                    value: 0
                },
                {
                    label: '计划报送',
                    value: 2
                }
            ];
        }
    }

    reportTypeChanged(type): void {
        switch (type) {
            case 0:
                this.getReportSubmitData(this.mngType);
                break;
            case 1:
                this.getPublishData(this.mngType);
                break;
            case 2:
                this.getPlanSubmitData(this.mngType);
                break;
        }
    }

    /**
     * 数据报送列表数据
     * @param mngType 0-水调中心 1-管理处
     */
    getReportSubmitData(mngType): void {
        if (mngType === 0) {
            this.fetchService.post('muw/submit/queryAll', {}).subscribe(res => {
                // this.tableData = res;
                if (Array.isArray(res)) {
                    this.tableData = res;
                }
            });
        } else if (mngType === 1) {
            this.fetchService.post('muw/submit/queryManage', {manageId: this.manageId}).subscribe(res => {
                // this.tableData = res;
                if (Array.isArray(res)) {
                    res.forEach(item => {
                        item['isFinish'] = item['statu'] > 1;
                    });
                    this.tableData = res;
                }
            });
        }
    }

    /**
     * 数据发布列表数据
     * @param mngType 0-水调中心 1-管理处
     */
    getPublishData(mngType): void {
        const params = mngType === 0 ? {} : {manageId: this.manageId};
        this.tableData = [];
    }

    /**
     * 计划报送列表数据
     * @param mngType 0-水调中心 1-管理处
     */
    getPlanSubmitData(mngType): void {
        const params = mngType === 0 ? {} : {manageId: this.manageId};
        this.fetchService.post('plan/wrsSchedulePlan/progress', params).subscribe(res => {
            // this.tableData = res;
            if (Array.isArray(res)) {
                res.forEach(item => {
                    if (item['data_type']) {
                        item['name'] = `${item['cycle']}年${item['m']}月${XunHash[item['data_type']]}旬用水计划`;
                        item['time'] = `${item['cycle']}年${item['m']}月${XunHash[item['data_type']]}旬`;
                    } else if (item['m'] === 'd') {
                        item['name'] = `${item['cycle']}年冬灌用水计划`;
                        item['time'] = `${item['cycle']}年冬`;
                    } else {
                        item['name'] = `${item['cycle']}年${item['m']}月用水计划`;
                        item['time'] = `${item['cycle']}年${item['m']}月`;
                    }
                    item['progress'] = `${item['qty']}/12`;
                    if (mngType === 0) {
                        item['isFinish'] = item['qty'] === 12;
                    } else {
                        item['isFinish'] = !!item['qty'];
                    }
                });
                this.tableData = res;
            }
        });
    }

    /**
     * 一键催报
     * @param data 当前行数据
     */
    broadcast(data): void {
        if (this.type === 0) {
            const params = data.type === '4' ? {type: 3} : {};   //  用水量
            this.fetchService
                .post(reportUrls[data.type], params)
                .subscribe(res => {
                    this.message.success('催报成功！');
                });
        } else if (this.type === 2) {
            const params = {
                "apicode": "18",
                "cycle": data.cycle,
                "m": data.m,
                "dataType":	data['data_type'] || null,
            };
            this.fetchService.post<any>('plan/wrsReportData/waterPlanInform', params).subscribe(res => {
                this.message.success(`催报成功`);
            });
        }
    }

    redirected(): void {
        this.modal.destroy();
        if (this.type === 0) {
            setTimeout(() => this.router.navigate(["/data-report/water-report"]));
        } else if (this.type === 2) {
            setTimeout(() => this.router.navigate(["/dispatch-plan/mng-report"]));
        }
    }

    directToOverview(): void {
        this.modal.destroy();
        setTimeout(() => this.router.navigate(["/data-report/overview"]));
    }
}
